package com.clkj.xiuzheng.service.shangcheng;

import com.clkj.xiuzheng.dao.shangcheng.ScDiscussionMapper;
import com.clkj.xiuzheng.pojo.shangcheng.ProductDiscussion;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;

@Service
public class ScDiscussionServiceImpl implements ScDiscussionService{

    @Autowired
    private ScDiscussionMapper scDiscussionMapper;

    @Override
    public PageInfo getDiscussionByCompanyId(Integer companyId, Integer parentId, Integer productId, Integer discussionType, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProductDiscussion> productDiscussions = scDiscussionMapper.getDiscussionByCompanyId(companyId,parentId,productId,discussionType);
        return new PageInfo<>(productDiscussions);
    }

    @Override
    public void addDiscussion(ProductDiscussion productDiscussion) {
        scDiscussionMapper.insertSelective(productDiscussion);
    }

    @Override
    public void deleteDiscussion(String Ids) {
        String[] ids = Ids.split(",");
        Example example = new Example(ProductDiscussion.class);
        example.createCriteria().andIn("discussionId", Arrays.asList(ids));
        List<ProductDiscussion> productDiscussions = scDiscussionMapper.selectByExample(example);
        //如果是提问 要把提问的回答一起删掉
        for (ProductDiscussion productDiscussion  : productDiscussions){
            if (productDiscussion.getDiscussionParentId() == 0){
                Example example1 = new Example(ProductDiscussion.class);
                example1.createCriteria().andEqualTo("discussionParentId", productDiscussion.getDiscussionId());
                scDiscussionMapper.deleteByExample(example1);
            }
        }
        Example example1 = new Example(ProductDiscussion.class);
        Example.Criteria criteria = example1.createCriteria();
        criteria.andIn("discussionId",Arrays.asList(ids));
        scDiscussionMapper.deleteByExample(example1);
    }

    @Override
    public void topDiscussion(String Ids, Integer isTop) {
        String[] ids = Ids.split(",");
        Example example = new Example(ProductDiscussion.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("discussionId",Arrays.asList(ids));
        ProductDiscussion productDiscussion = new ProductDiscussion();
        productDiscussion.setIsTop(isTop);
        scDiscussionMapper.updateByExampleSelective(productDiscussion,example);
    }
}
